package wcg;

import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.util.LinkedList;


/**
 * winchkgプログラム全体からアクセスできるパラメータ
 * グローバル変数の集まり
 * @author ikeda
 *
 */
public  class winchkgProperty extends winchkgProp {
	
	private static final String FmtWindowPosX = "Window position X";
	private static final int DefaultWindowPosX = 10;
	public  static int WindowPosX = DefaultWindowPosX;
	
	private static final String FmtWindowSizeX = "Window Size X";
	private static final int DefaultWindowSizeX = 800;
	public  static int WindowSizeX = DefaultWindowSizeX;
	
	private static final String FmtWindowPosY = "Window position Y";
	private static final int DefaultWindowPosY = 10;
	public  static int WindowPosY = DefaultWindowPosY;
	
	private static final String FmtWindowSizeY = "Window Size Y";
	private static final int DefaultWindowSizeY = 600;
	public  static int WindowSizeY = DefaultWindowSizeY;
	
	private static final String FmtPort = "Port";
	private static final int DefaultPort = 7200;
	public  static int Port = DefaultPort;
	
	private static final String FmtWriteFileFolder = "WriteFileFolder";
	//private static final String DefaultWriteFileFolder = ".\\";
	private static final String DefaultWriteFileFolder = "";
	public static String WriteFileFolder = DefaultWriteFileFolder;
	
	//-------------------- Graphic View ------------------------------
	private static final String FmtTimeChartColor = "Time Chart Color";
	private static final int    DefaultTimeChartColor = 0;
	public  static int  TimeChartColor = DefaultTimeChartColor;
	
	private static final String FmtShowMean = "Show Mean";
	private static final boolean DefaultShowMean = false;
	public  static boolean ShowMean = DefaultShowMean;
	
	private static final String FmtShowChannel = "Show Channel";
	private static final boolean DefaultShowChannel = true;
	public  static boolean  ShowChannel = DefaultShowChannel;
	
	private static final String FmtShowStation = "Show Station";
	private static final boolean DefaultShowStation = true;
	public  static boolean ShowStation = DefaultShowStation;
	
	private static final String FmtShowComponent = "Show Component";
	private static final boolean DefaultShowComponent = true;
	public  static boolean ShowComponent = DefaultShowComponent;
	
	private static final String FmtShowTimeSamplePoint = "Show Time and Sample point";
	private static final boolean DefaultShowTimeSamplePoint = false;
	public  static boolean ShowTimeSamplePoint = DefaultShowTimeSamplePoint;
	
	private static final String FmtShowAmplitudeLabel = "Show Amplitude Label";
	private static final boolean DefaultShowAmplitudeLabel = true;
	public  static boolean ShowAmplitudeLabel = DefaultShowAmplitudeLabel;
	
	private static final String FmtShowAmplitudeLabelAxis = "Show Amplitude Label and Axis";
	private static final boolean DefaultShowAmplitudeLabelAxis = true;
	public  static boolean ShowAmplitudeLabelAxis = DefaultShowAmplitudeLabelAxis;
	
	private static final String FmtClipWave = "Clip wave";
	private static final boolean DefaultClipWave = false;
	public  static boolean ClipWave = DefaultClipWave;
	
	private static final String FmtShowCursorTime = "Show Cursor time";
	private static final boolean DefaultShowCursorTime = true;
	public  static boolean ShowCursorTime = DefaultShowCursorTime;
	
	private static final String FmtGraphicX = "Graphic X";
	private static final int DefaultGraphicX = 250;
	public static int GraphicX = DefaultGraphicX;
	
	private static final String FmtPhysicalQuantityFormat = "PhysicalQuantityFormat";
	private static final String DefaultPhysicalQuantityFormat = "%.3e";
	public static String PhysicalQuantityFormat = DefaultPhysicalQuantityFormat;
	
	private static final String FmtYAxisDrawCenter = "YScaleDrawCenter";
	private static final boolean DefaultYAxisDrawCenter = true;
	public  static boolean YAxisDrawCenter = DefaultYAxisDrawCenter;
	
	private static final String FmtYAxisDrawControlHandle = "YScaleDrawControlHandle";
	private static final boolean DefaultYAxisDrawControlHandle = true;
	public  static boolean YAxisDrawControlHandle = DefaultYAxisDrawControlHandle;
	
	private static final String FmtYAxisLabelFontMax = "YAxisLabelFontMax";
	private static final int DefaultYAxisLabelFontMax = 12;
	public static int YAxisLabelFontMax = DefaultYAxisLabelFontMax;

	private static final String FmtYAxisLabelFontMin = "YAxisLabelFontMin";
	private static final int DefaultYAxisLabelFontMin = 8;
	public static int YAxisLabelFontMin = DefaultYAxisLabelFontMin;

	private static final String FmtYAxisLabelNumberMax = "YAxisLabelNumberMax";
	private static final int DefaultYAxisLabelNumberMax = 21;
	public static int YAxisLabelNumberMax = DefaultYAxisLabelNumberMax;

	
	private static final String FmtXAxisDrawXBar = "XAxisDrawXBar";
	private static final boolean DefaultXAxisDrawXBar = true;
	public  static boolean XAxisDrawXBar = DefaultXAxisDrawXBar;

	private static final String FmtXAxisDrawClock = "XAxisDrawClock";
	private static final boolean DefaultXAxisDrawClock = true;
	public  static boolean XAxisDrawClock = DefaultXAxisDrawClock;

	
	// --------------- Edition control ---------------------
	private static final String FmtEditionControl = "EditionControl";
	public static final int EditionControlLite = 0; // minimum function
	public static final int EditionControlFull = 255; // no limit function
	private static final int DefaultEditionControl = EditionControlLite;
	public static int EditionControl = DefaultEditionControl;
	
	// --------------- Print control ---------------------
	private static final String FmtPrintColorAlwaysWhite = "PrintColorAlwaysWhite";
	private static final boolean DefaultPrintColorAlwaysWhite = true; 
	public static boolean PrintColorAlwaysWhite = DefaultPrintColorAlwaysWhite;
	
	// --------------- Update control ---------------------
	private static final String FmtUpdateBeta = "Update beta";
	private static final boolean DefaultUpdateBeta = false; 
	public static boolean UpdateBeta = DefaultUpdateBeta;

	private static final String FmtUpdateAuto = "Update auto";
	private static final boolean DefaultUpdateAuto = false; 
	public static boolean UpdateAuto = DefaultUpdateAuto;
	
	private static final String FmtUpdateCheckDate = "Update check date";
	private static final long DefaultUpdateCheckDate = 0; 
	public static long UpdateCheckDate = DefaultUpdateCheckDate;
	
	//----------------- Look and feel ---------------------------
	private static final String FmtLookAndFeel = "Look and Feel";
	private static final String DefaultLookAndFeel = "Metal"; 
	public static String LookAndFeel = DefaultLookAndFeel;
	
	//-------------------- Time series View ------------------------------
	private static final String FmtTimeSeriesExportImageDir = "TimeSerieseExportImageDir";
	public  static String TimeSeriesExportImageDir = null;
	
	//-------------------- Spectrum View ------------------------------
	private static final String FmtSpectrumColor = "Spectrum Color";
	private static final int    DefaultSpectrumColor = 0;
	public  static int  SpectrumColor = DefaultSpectrumColor;
	
	public  static final String FmtSpectrumExportImageDir = "SpectrumExportImageDir";
	public  static String SpectrumExportImageDir = null;

	//-------------------- SNR View ------------------------------
	private static final String FmtSNRColor = "SNR Color";
	private static final int    DefaultSNRColor = 0;
	public  static int  SNRColor = DefaultSNRColor;
	
	public  static final String FmtDynamicRangeExportImageDir = "DynamicRangeExportImageDir";
	public  static String DynamicRangeExportImageDir = null;
	
	//-------------------- windatabase ------------------------------
	public static final int RecentFilesMax = 10;
	private static final String FmtRecentFiles = "RecentFiles";
	public static String[] RecentFiles = new String[RecentFilesMax];;
	
	
	//----------------------------------------------------------------
	
	public winchkgProperty(String propfile) {
		super(propfile);
	}
	public  void LoadSaveProperty(boolean save){
		if(save){
			//WinChkgProperty.setString(FmtWriteFileFolder, WinUdp2File.getPath());
			setString(FmtWriteFileFolder, WriteFileFolder);
			setInt(FmtPort, Port);
			setInt(FmtWindowSizeX,WindowSizeX);
			setInt(FmtWindowSizeY,WindowSizeY);
			setInt(FmtWindowPosX,WindowPosX);
			setInt(FmtWindowPosY,WindowPosY);
			//---Graphics view------------------------------------
			setInt(FmtTimeChartColor,TimeChartColor);
			setBoolean(FmtShowMean,ShowMean);
			setBoolean(FmtShowChannel,ShowChannel);
			setBoolean(FmtShowStation,ShowStation);
			setBoolean(FmtShowComponent,ShowComponent);
			setBoolean(FmtShowTimeSamplePoint,ShowTimeSamplePoint);
			setBoolean(FmtShowAmplitudeLabel,ShowAmplitudeLabel);
			setBoolean(FmtShowAmplitudeLabelAxis,ShowAmplitudeLabelAxis);
			setBoolean(FmtClipWave,ClipWave);
			setBoolean(FmtShowCursorTime,ShowCursorTime);
			setInt(FmtGraphicX,GraphicX);
			setString(FmtPhysicalQuantityFormat,PhysicalQuantityFormat);
			
			//---Edition control------------------------------------
			setInt(FmtEditionControl,EditionControl);
			// --------------- Print control ---------------------
			setBoolean(FmtPrintColorAlwaysWhite,PrintColorAlwaysWhite);
			// --------------- Update control ---------------------
			setBoolean(FmtUpdateBeta,UpdateBeta);
			setBoolean(FmtUpdateAuto,UpdateAuto);
			setLong(FmtUpdateCheckDate,UpdateCheckDate);
			
			// --------------- Look and Feel ---------------------
			setString(FmtLookAndFeel,LookAndFeel);
			
			// --------------- Time Series ---------------------
			setString( FmtTimeSeriesExportImageDir,TimeSeriesExportImageDir);

			setBoolean(FmtYAxisDrawCenter,YAxisDrawCenter);
			setBoolean(FmtYAxisDrawControlHandle,YAxisDrawControlHandle);
			setInt(FmtYAxisLabelFontMax,YAxisLabelFontMax);
			setInt(FmtYAxisLabelFontMin,YAxisLabelFontMin);
			setInt(FmtYAxisLabelNumberMax,YAxisLabelNumberMax);
			setBoolean(FmtXAxisDrawXBar,XAxisDrawXBar);
			setBoolean(FmtXAxisDrawClock,XAxisDrawClock);
			
			
			//---Spectrum view------------------------------------
			setInt(FmtSpectrumColor,SpectrumColor);
			setString( FmtSpectrumExportImageDir,SpectrumExportImageDir);
			
			//---SNR view------------------------------------
			setInt(FmtSNRColor,SNRColor);
			setString( FmtDynamicRangeExportImageDir,DynamicRangeExportImageDir);
			
			// --------------- Recent Files ---------------------
			for(int i=0;i<RecentFilesMax;i++){
				String fmt = String.format("%s%d", FmtRecentFiles,i);
				setString(fmt,RecentFiles[i]);
			}

			Save();
		}
		else{ // loading
			Load();
			WriteFileFolder = new String(getString(FmtWriteFileFolder,DefaultWriteFileFolder));
			Port = getInt(FmtPort,DefaultPort);
			
			WindowSizeX =  getInt(FmtWindowSizeX,DefaultWindowSizeX);
			WindowSizeY =  getInt(FmtWindowSizeY,DefaultWindowSizeY);
			WindowPosX = getInt(FmtWindowPosX,DefaultWindowPosX);
			WindowPosY = getInt(FmtWindowPosY,DefaultWindowPosY);
			//---Graphics view------------------------------------
			TimeChartColor = getInt(FmtTimeChartColor,DefaultTimeChartColor);
			ShowMean = getBoolean(FmtShowMean,DefaultShowMean);
			ShowChannel = getBoolean(FmtShowChannel,DefaultShowChannel);
			ShowStation = getBoolean(FmtShowStation,DefaultShowStation);
			ShowComponent = getBoolean(FmtShowComponent,DefaultShowComponent);
			ShowTimeSamplePoint = getBoolean(FmtShowTimeSamplePoint,DefaultShowTimeSamplePoint);
			ShowAmplitudeLabel = getBoolean(FmtShowAmplitudeLabel,DefaultShowAmplitudeLabel);
			ShowAmplitudeLabelAxis = getBoolean(FmtShowAmplitudeLabelAxis,DefaultShowAmplitudeLabelAxis);
			ClipWave = getBoolean(FmtClipWave,DefaultClipWave);
			ShowCursorTime = getBoolean(FmtShowCursorTime,DefaultShowCursorTime);
			GraphicX = getInt(FmtGraphicX,DefaultGraphicX);
			PhysicalQuantityFormat = getString(FmtPhysicalQuantityFormat,DefaultPhysicalQuantityFormat);
			//---Edition control------------------------------------
			int edit = getInt(FmtEditionControl,DefaultEditionControl);
			setEditionControl(edit);
			// --------------- Print control ---------------------
			PrintColorAlwaysWhite = getBoolean(FmtPrintColorAlwaysWhite,DefaultPrintColorAlwaysWhite);
			// --------------- Update control ---------------------
			UpdateBeta = getBoolean(FmtUpdateBeta,DefaultUpdateBeta);
			UpdateAuto = getBoolean(FmtUpdateAuto,DefaultUpdateAuto);
			UpdateCheckDate = getLong(FmtUpdateCheckDate,DefaultUpdateCheckDate);
			// --------------- Look and Feel ---------------------
			LookAndFeel = getString(FmtLookAndFeel,DefaultLookAndFeel);
			
			// --------------- Time Series ---------------------
			TimeSeriesExportImageDir = getString( FmtTimeSeriesExportImageDir,".\\");
					
			YAxisDrawCenter = getBoolean(FmtYAxisDrawCenter,DefaultYAxisDrawCenter);
			YAxisDrawControlHandle = getBoolean(FmtYAxisDrawControlHandle,DefaultYAxisDrawControlHandle);
			YAxisLabelFontMax = getInt(FmtYAxisLabelFontMax,DefaultYAxisLabelFontMax);
			YAxisLabelFontMin = getInt(FmtYAxisLabelFontMin,DefaultYAxisLabelFontMin);
			YAxisLabelNumberMax = getInt(FmtYAxisLabelNumberMax,DefaultYAxisLabelNumberMax);
			XAxisDrawXBar = getBoolean(FmtXAxisDrawXBar,DefaultXAxisDrawXBar);
			XAxisDrawClock = getBoolean(FmtXAxisDrawClock,DefaultXAxisDrawClock);

			
			//---Spectrum view------------------------------------
			SpectrumColor = getInt(FmtSpectrumColor,DefaultSpectrumColor);
			SpectrumExportImageDir = getString( FmtSpectrumExportImageDir,".\\");
			
			//---SNR view------------------------------------
			SNRColor = getInt(FmtSNRColor,DefaultSNRColor);
			DynamicRangeExportImageDir = getString( FmtDynamicRangeExportImageDir,".\\");
			
			// --------------- Recent Files ---------------------
			for(int i=0;i<RecentFilesMax;i++){
				String fmt = String.format("%s%d", FmtRecentFiles,i);
				RecentFiles[i] = getString(fmt,"");
			}
		}
	}
	
	/**
	 * エディション毎の機能フラグ
	 */
	private static final long serialVersionUID = 1L;

	public static boolean availableTimechartManiac 			= false;
	public static boolean availableSpectrumPane 		= false;
	public static boolean availableWincheker 				= false;
	public static boolean availableSplitter 				= false;
	public static boolean availableWinConverter 			= false;
	public static boolean availableDynamicRangePane 		= false;
	public static boolean availableExportSigma 				= false;
	public static boolean availableSplineInterporation 		= false;
	public static boolean availableSignalProcessingPane 	= false;
	public static boolean availableProtocolTest 			= false;
	public static boolean availableImportCSV				= false;
	public static boolean availableIrigTools				= false;
	public static boolean availableDeveloping				= false;
	public static boolean availableExportBin				= false;
	public static boolean availableExportWin				= false;
	public static boolean availableEditionDialog			= false;
	
	private static String[][] Availables =  {
		{ "availableEditionDialog"			, "0" },	// エディション変更をダイアログでできる
		{ "availableSpectrumPane"			, "1"},		// スペクトラムビュー 
		{ "availableDynamicRangePane"		, "2" },	// ダイナミックレンジ
		{ "availableExportSigma"			, "2" },	// エクスポート　シグマ
		{ "availableWincheker"				, "3"},		// winチェッカ－
		
		{ "availableSplitter"				, "4" },	// winスプリッター
		{ "availableWinConverter"			, "5" },	// winコンバータ
		{ "availableTimechartManiac" 		, "5"},		// タイムチャートビューのマニアックメニュ
		{ "availableExportBin"				, "5" },	// win エクスポート バイナリ
		{ "availableExportWin"				, "5" },	// win エクスポート win
		{ "availableSplineInterporation"	, "6" },	// スプラインインターポレーション
		{ "availableIrigTools"				, "7" },	// Irig ツール
		{ "availableSignalProcessingPane"	, "10" },	// 信号処理ビュー

		{ "availableProtocolTest"			, "8" },	// プロトコルテスト
		{ "availableImportCSV"				, "9" },	// CSVインポート
		{ "availableDeveloping"				, "30" },	// 開発用
		{ "end"								, "31" },
	};
	/**
	 * available云々の名前の機能が有効かどうかを返す。
	 * ヘルプファイルのエディション毎の表示制御につかう。
	 * @param name
	 * @return
	 */
	public static boolean isAvailable(String name){
		int b = getEditionBit(name);
		if((EditionControl & b) != 0) return true;
		return false;
	}
	/**
	 *  Intなedition を機能フラグに変換
	 * @param edition
	 */
	public static  void setEditionControl(int edition){
		EditionControl 				= edition;

		availableEditionDialog	    = editionBitCheck("availableEditionDialog");
		availableSpectrumPane  		= editionBitCheck("availableSpectrumPane");
		availableDynamicRangePane   = editionBitCheck("availableDynamicRangePane");
		availableExportSigma        = editionBitCheck("availableExportSigma");
		availableWincheker 			= editionBitCheck("availableWincheker");
		
		availableSplitter			= editionBitCheck("availableSplitter");
		availableWinConverter		= editionBitCheck("availableWinConverter");
		availableExportBin      	= editionBitCheck("availableExportBin");
		availableExportWin      	= editionBitCheck("availableExportWin");
		availableTimechartManiac    = editionBitCheck("availableTimechartManiac");
		availableSplineInterporation= editionBitCheck("availableSplineInterporation");
		availableIrigTools			= editionBitCheck("availableIrigTools");
		
		availableProtocolTest		= editionBitCheck("availableProtocolTest");
		availableImportCSV			= editionBitCheck("availableImportCSV");
		availableSignalProcessingPane= editionBitCheck("availableSignalProcessingPane");
		
		availableDeveloping			= editionBitCheck("availableDeveloping");
	}
	/** 
	 * edition名と値の組み合わせ
	 */
	static String[] EditionNames = { "elementary" , "basic", 	"users", 	"superusers", 	"developers", 	"God" };
	static int[]    EditionDef   = { 0x0, 			0x3, 		0x33,		0xff, 			0x7ff , 		0x7fffffff };
	/**
	 * エディションの名前を列挙する
	 * @return
	 */
	public String[] enumEditonNames(){
		return EditionNames;
	}
	/**
	 * 今のエディション名を得る
	 * @return
	 */
	public static String getEditionNames(){
		for(int i=0;i<EditionDef.length;i++){
			if(EditionDef[i] == EditionControl){
				return EditionNames[i];
			}
		}
		return new String("Unknown");
	}
	/**
	 * 名前でeditionをセットする
	 * @param edition
	 * @return
	 */
	public static boolean setEditionControl(String edition){
		
		for(int i=0;i<EditionDef.length;i++){
			if(edition.compareToIgnoreCase(EditionNames[i]) == 0){
				setEditionControl(EditionDef[i]);
				return true;
			}
		}
		return false;
	}
	/**
	 * ビットの名前からedition bitを返す。
	 * @param name
	 * @return
	 */
	private static int getEditionBit(String name){
		int i = 0;
		while(true){
			if(Availables[i][0].compareToIgnoreCase("end")==0) break;
			if(Availables[i][0].compareToIgnoreCase(name)==0){
				int num = Integer.valueOf(Availables[i][1]);
				return  (1<<num);
			}
			i++;
		}
		System.err.println("Error help processing "+ name);
		return 0; // エラー
	}
	/**
	 * ビットにマップする
	 * @param name
	 * @return
	 */
	private static boolean editionBitCheck(String name){
		int i = 0;
		while(true){
			if(Availables[i][0].compareToIgnoreCase("end")==0) break;
			if(Availables[i][0].compareToIgnoreCase(name)==0){
				int num = Integer.valueOf(Availables[i][1]);
				return editionBitCheck(num);
			}
			i++;
		}
		return false;
	}
	/**
	 * ビットにマップする
	 * @param b
	 * @return
	 */
	private static boolean editionBitCheck(int b){
		if((EditionControl & (1<<b)) != 0) return true;
		return false;
	}
	
}
